package cn.csc.module.product.controller.admin.tag;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.tag.vo.*;
import cn.csc.module.product.convert.tag.SpuTagConvert;
import cn.csc.module.product.dal.dataobject.tag.SpuTagDO;
import cn.csc.module.product.service.tag.SpuTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 商品标签")
@RestController
@RequestMapping("/product/spu-tag")
@Validated
public class SpuTagController {

    @Resource
    private SpuTagService spuTagService;

    @PostMapping("/create")
    @ApiOperation("创建商品标签")
    @PreAuthorize("@ss.hasPermission('product:spu-tag:create')")
    public CommonResult<BigInteger> createSpuTag(@Valid @RequestBody SpuTagCreateReqVO createReqVO) {
        return success(spuTagService.createSpuTag(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新商品标签")
    @PreAuthorize("@ss.hasPermission('product:spu-tag:update')")
    public CommonResult<Boolean> updateSpuTag(@Valid @RequestBody SpuTagUpdateReqVO updateReqVO) {
        spuTagService.updateSpuTag(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商品标签")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-tag:delete')")
    public CommonResult<Boolean> deleteSpuTag(@RequestParam("id") BigInteger id) {
        spuTagService.deleteSpuTag(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商品标签")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-tag:query')")
    public CommonResult<SpuTagRespVO> getSpuTag(@RequestParam("id") BigInteger id) {
        SpuTagDO spuTag = spuTagService.getSpuTag(id);
        return success(SpuTagConvert.INSTANCE.convert(spuTag));
    }

    @GetMapping("/list")
    @ApiOperation("获得商品标签列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:spu-tag:query')")
    public CommonResult<List<SpuTagRespVO>> getSpuTagList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<SpuTagDO> list = spuTagService.getSpuTagList(ids);
        return success(SpuTagConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得商品标签分页")
    @PreAuthorize("@ss.hasPermission('product:spu-tag:query')")
    public CommonResult<PageResult<SpuTagRespVO>> getSpuTagPage(@Valid SpuTagPageReqVO pageVO) {
        PageResult<SpuTagDO> pageResult = spuTagService.getSpuTagPage(pageVO);
        return success(SpuTagConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出商品标签 Excel")
    @PreAuthorize("@ss.hasPermission('product:spu-tag:export')")
    @OperateLog(type = EXPORT)
    public void exportSpuTagExcel(@Valid SpuTagExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SpuTagDO> list = spuTagService.getSpuTagList(exportReqVO);
        // 导出 Excel
        List<SpuTagExcelVO> datas = SpuTagConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品标签.xls", "数据", SpuTagExcelVO.class, datas);
    }

}
